package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.manager.OtherPurchaseRecordManager;
import com.jiantong.work.model.OtherPurchaseRecord;
import com.jiantong.work.service.OtherPurchaseRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by niufei on 2017/4/10.
 *
 */
@Slf4j
@Service
@RpcProvider
public class OtherPurchaseRecordWriteServiceImpl implements OtherPurchaseRecordWriteService{

    private final OtherPurchaseRecordManager otherPurchaseRecordManager;

    @Autowired
    public OtherPurchaseRecordWriteServiceImpl(OtherPurchaseRecordManager otherPurchaseRecordManager) {
        this.otherPurchaseRecordManager = otherPurchaseRecordManager;
    }

    @Override
    public Response<Map<String, Object>> create(OtherPurchaseRecord otherPurchaseRecord , Integer toStatus) {
        try{
            return Response.ok(otherPurchaseRecordManager.create(otherPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to create otherPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("otherPurchaseRecord.create.failed");
        }
    }

    @Override
    public Response<Map<String, Object>> update(OtherPurchaseRecord otherPurchaseRecord , Integer toStatus) {
        try{
            return Response.ok(otherPurchaseRecordManager.update(otherPurchaseRecord,toStatus));
        }catch (Exception e){
            log.error("failed to update otherPurchaseRecord error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("otherPurchaseRecord.create.failed");
        }
    }

    @Override
    public Boolean update(OtherPurchaseRecord otherPurchaseRecord) {
        return otherPurchaseRecordManager.update(otherPurchaseRecord);
    }
}
